package com.affectu.task.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.faces.FacesMessages;

import com.affectu.category.Category;
import com.affectu.dao.DAOBase;
import com.affectu.dao.pagination.CategoryTaskDataModel;
import com.affectu.message.Message;
import com.affectu.search.SearchAction;
import com.affectu.task.Bid;
import com.affectu.task.InnerTask;
import com.affectu.task.Task;

/*
 * 2008-9-1
 * @author daniel nathan
 */
@Name("taskIndexAction")
// @Scope(ScopeType.SESSION)
public class Index extends DAOBase {

	public String searchSubCategory(String categoryName) {
		SearchAction searchAction = new SearchAction();
		searchAction.setKeywords(categoryName);
		searchAction.setSearchType("task");
		searchAction.luceneSearch();
		this.getSessionScope().put("searchAction", searchAction);
		return "/search/result.xhtml";
	}

	@SuppressWarnings("unchecked")
	public List<Message> getNewMessages() {
		List<Message> newMessages = new ArrayList<Message>();
		try {
			newMessages = em
					.createQuery(
							"select m from Message m where m.receiver=:receiver and m.status=:status order by m.sendDate desc")
					.setParameter("receiver", this.getCurrentUser())
					.setParameter("status", Message.Status.NEW).list();
		} catch (Exception ex) {
			this.logException(ex);
		}

		return newMessages;
	}

	@SuppressWarnings("unchecked")
	public List<Task> getOwnTasks() {
		List<Task> ownTasks = new ArrayList<Task>();
		try {
			ownTasks = em
					.createSQLQuery(
							"select t.* from InnerTask t where t.userId=:userId order by t.publishDate desc")
					.addEntity("t", InnerTask.class).setParameter("userId",
							this.getCurrentUser().getId()).list();// 默认行为
		} catch (Exception ex) {
			this.logException(ex);
		}

		return ownTasks;
	}

	@Factory(value = "taskIndexAction_hotTasks", scope = ScopeType.PAGE)
	public List<Task> getHotTasks() {
		List<Task> tasks = new ArrayList<Task>();
		try {
			tasks.addAll(this.getHotOuterTask());
			tasks.addAll(this.getHotInnerTask());
			Collections.sort(tasks);
			if (tasks.size() >= 10) {
				tasks = tasks.subList(0, 10);
			} else {
				tasks = tasks.subList(0, tasks.size());
			}

		} catch (Exception ex) {
			this.logException(ex);
		}
		return tasks;
	}

	@Factory(scope = ScopeType.APPLICATION)
	public static CategoryTaskDataModel getPagedTasksByCategory(
			Category category) {
		return new CategoryTaskDataModel(category);
	}

	public String selectBidder(Bid bid) {
		try {
			bid.setStatus(Bid.Status.ACCEPTED);
			em.merge(bid.getBelongToTask());
			FacesMessages.instance()
					.add(bid.getOwner().getNickName() + "已经中标!");
		} catch (Exception ex) {
			this.logException(ex);
		}
		return "";
	}

	@SuppressWarnings("unchecked")
	public List<Bid> getAcceptedBids() {
		List<Bid> bids = new ArrayList<Bid>();
		try {
			bids = em
					.createQuery(
							"select b from Bid b where b.owner=:owner and b.status=:status")
					.setParameter("owner", this.getCurrentUser()).setParameter(
							"status", Bid.Status.ACCEPTED).list();
		} catch (Exception ex) {
			this.logException(ex);
		}

		return bids;
	}

	@SuppressWarnings("unchecked")
	public List<Task> getHotOuterTask() {
		List<Task> tasks = new ArrayList<Task>();
		try {
			tasks = em.createQuery("from OuterTask o order by o.id desc")
					.setFirstResult(1).setMaxResults(10).list();
		} catch (Exception ex) {
			this.logException(ex);
		}
		return tasks;
	}

	@SuppressWarnings("unchecked")
	public List<Task> getHotInnerTask() {
		List<Task> tasks = new ArrayList<Task>();
		try {
			tasks = em.createQuery("from InnerTask i order by i.id desc")
					.setFirstResult(1).setMaxResults(10).list();
		} catch (Exception ex) {
			this.logException(ex);
		}
		return tasks;
	}

}
